//
//  WYStatusRetweetView.swift
//  WYWeibo
//
//  Created by wangyi on 16/7/14.
//  Copyright © 2016年 wangyi. All rights reserved.
//

import UIKit
import SnapKit

class WYStatusRetweetView: UIView {
    var retweetViewBottoM: Constraint?
    var statusViewModel: WYStatusViewModel? {
        
        didSet{
            //设置专发内容
            contentLabel.text = statusViewModel?.retweetContent
           //卸载上一次底部约束
            retweetViewBottoM?.uninstall()
            pictureView.picUrls = statusViewModel?.status?.retweeted_status?.pic_urls
            //判断是否有转发微博配图
            if let count = statusViewModel?.status?.retweeted_status?.pic_urls?.count where count > 0 {
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.retweetViewBottoM = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
                })
                pictureView.hidden = false
            } else {
            
                pictureView.hidden = true
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.retweetViewBottoM = make.bottom.equalTo(contentLabel).offset(HomeCellMargin).constraint
                })
            }
        }
    }
    
    
    //MARK -- 懒加载控件
    private lazy var contentLabel: UILabel = {
        
        let label = UILabel(textColor: UIColor.grayColor(), fontSize: 14)
        //多行显示
        label.numberOfLines = 0
        return label
    }()
    
    //转发微博配图
    private lazy var pictureView: WYStatusPicView = {
        
        let collectionView = WYStatusPicView()
        collectionView.backgroundColor = self.backgroundColor
        return collectionView
        
        
    }()
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
   private func setUpUI() {
    
        contentLabel.backgroundColor = UIColor.grayColor()
        //添加控件
        addSubview(contentLabel)
        addSubview(pictureView)
    
    
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(HomeCellMargin)
            make.leading.equalTo(self).offset(HomeCellMargin)
            make.trailing.equalTo(self).offset(-HomeCellMargin)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(HomeCellMargin)
    }
        //关键的约束
        //转发微博视图的底部约束等于我们转发内容的 lebel 底部添加一个间距
        self.snp_makeConstraints { (make) -> Void in
           self.retweetViewBottoM = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
        }
    
    }
}
